package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.system.ISysCarmeraConfigMapper;
import com.wejoy.video.middle.station.data.domain.system.SysCarmeraConfig;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.ISysCarmeraConfigService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 算法配置
 *
 * @author miracle_j
 * @date 2024年04月25日 16:10:43
 */
@Service
public class SysCarmeraConfigServiceImpl implements ISysCarmeraConfigService {
    @Autowired
    private ISysCarmeraConfigMapper sysCarmeraConfigMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(SysCarmeraConfig req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(SysCarmeraConfig req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return sysCarmeraConfigMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(SysCarmeraConfig sysCarmeraConfig) {
        WJAssert.notNull(sysCarmeraConfig, "入参对象不能为空");
        WJAssert.notNull(sysCarmeraConfig.getId(), "id参数错误");
        SysCarmeraConfig where = new SysCarmeraConfig();
        where.setId(sysCarmeraConfig.getId());
        return sysCarmeraConfigMapper.updateSelective(sysCarmeraConfig, where);
    }

    @Override
    public int getCount(SysCarmeraConfig sysCarmeraConfig) {
        checkStringLength(sysCarmeraConfig, true);
        return sysCarmeraConfigMapper.getCount(sysCarmeraConfig);
    }

    @Override
    public SysCarmeraConfig getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        SysCarmeraConfig where = new SysCarmeraConfig();
        where.setId(id);
        return sysCarmeraConfigMapper.getOne(where);
    }


    @Override
    public SysCarmeraConfig getOneByIdAndSceneTypeAndAbilityType(Long id, String sceneType, String abilityType) {
        WJAssert.notNull(id, "id参数错误");
        SysCarmeraConfig where = new SysCarmeraConfig();
        where.setPointId(id);
        where.setSceneType(sceneType);
        where.setAbilityType(abilityType);
        return sysCarmeraConfigMapper.getOne(where);
    }

    @Override
    public List<SysCarmeraConfig> getList(SysCarmeraConfig sysCarmeraConfig) {
        checkStringLength(sysCarmeraConfig, true);
        return sysCarmeraConfigMapper.getList(sysCarmeraConfig);
    }

    @Override
    public List<SysCarmeraConfig> getPageList(SysCarmeraConfig sysCarmeraConfig) {
        checkStringLength(sysCarmeraConfig, true);
        return sysCarmeraConfigMapper.getPageList(sysCarmeraConfig);
    }

    @Override
    public void audit(Long id, String operator) {
        SysCarmeraConfig req = new SysCarmeraConfig();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        SysCarmeraConfig req = new SysCarmeraConfig();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        SysCarmeraConfig req = new SysCarmeraConfig();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(SysCarmeraConfig sysCarmeraConfig, boolean nullAble) {
        WJAssert.notNull(sysCarmeraConfig, "入参对象不能为空");
        WJAssert.limitMaxLength(sysCarmeraConfig.getSceneType(), true, 50, "算法场景类型不合法!");
        WJAssert.limitMaxLength(sysCarmeraConfig.getAbilityType(), true, 50, "算法能力类型不合法!");
        WJAssert.limitMaxLength(sysCarmeraConfig.getConfigJson(), true, 65535, "配置json不合法!");
        WJAssert.limitMaxLength(sysCarmeraConfig.getWinterConfigJson(), true, 65535, "冬季配置json不合法!");
        WJAssert.limitMaxLength(sysCarmeraConfig.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(sysCarmeraConfig.getRemark(), true, 255, "备注不合法!");
    }
}
